var VUE_UTIL = {
    BASE_API: "",
    getAxiosInstance: function() {
        var instance = axios.create({
            baseURL: VUE_UTIL.BASE_API,
            timeout: 5000,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
            }
        });
        //添加请求拦截器
        instance.interceptors.request.use(function (config) {
            // 在发送请求之前做些什么
            return config;
        }, function (error) {
            // 对请求错误做些什么
            // return Promise.reject(error);
        });

        // 添加响应拦截器
        instance.interceptors.response.use(function (response) {
            // 对响应数据做点什么
            return response;
        }, function (error) {
            // 对响应错误做点什么
            // return Promise.reject(error);
        });
        return instance
    },
    /**
     * 将请求参数对象转换成字符串格式
     * @param parameters
     * @param noFilter  如果为true，则不过滤 {}, '', -1 这三种值
     * @returns {string}
     */
    buildRequestParameters: function(parameters, noFilter) {
        var parametersArr = [];
        for (var index in parameters) {
            var temp
            try {
                if (typeof noFilter == 'undefined' || !noFilter) {
                    if (parameters.hasOwnProperty(index) && JSON.stringify(parameters[index]) !== '{}' && parameters[index] !== '' && parameters[index] !== -1) {
                        temp = parameters[index]
                        parametersArr.push(encodeURIComponent(index) + '=' + encodeURIComponent(temp))
                    }
                } else {
                    temp = parameters[index]
                    parametersArr.push(encodeURIComponent(index) + '=' + encodeURIComponent(temp))
                }
            } catch (e){
                console.log(e.message)
            }
        }
        return parametersArr.length > 0 ? '?' + parametersArr.join('&'): ''
    },

    /**
     * 将对象转为formData
     * @param postData
     * @param noFilter
     * @returns {*}
     */
    buildPostFormData: function(postData, noFilter) {
        var formData = new FormData()
        for(var index in postData) {
            if (postData.hasOwnProperty(index)) {
                try {
                    if (typeof noFilter == 'undefined' || !noFilter) {
                        if (JSON.stringify(postData[index]) !== '{}' && postData[index] !== '' && postData[index] !== -1) {
                            if(postData[index] instanceof Array || postData[index] instanceof  Object) {
                                formData.append(index, JSON.stringify(postData[index]))
                            } else {
                                formData.append(index, postData[index])
                            }
                        }
                    } else {
                        if(postData[index] instanceof Array || postData[index] instanceof  Object) {
                            formData.append(index, JSON.stringify(postData[index]))
                        } else {
                            formData.append(index, postData[index])
                        }
                    }
                } catch (e) {
                    console.log(e.message)
                }
            }
        }
        return formData
    },
    /**
     * 生成get请求的URL
     * @param url
     * @param parameters
     * @param noFilter
     */
    buildRequestUrl: function(url, parameters, noFilter) {
        return url + this.buildRequestParameters(parameters, noFilter)
    },
    /**
     * 获取citypicker插件的code
     * @param dom  dom元素
     * @returns {object}
     */
    getCode:function(dom){
        var obj = {}
        //获取地址code
        var cityData = wzERP.Common.getCityData(dom);
        if (cityData) {
            obj.provinceCode = cityData[1].value;
            obj.cityCode = cityData[2].value;
            obj.countyCode = cityData[3].value;
        }
        return obj
    },
    /**
     * 检测对象是否是空对象(不包含任何可读属性)。
     * 方法既检测对象本身的属性，也检测从原型继承的属性(因此没有使hasOwnProperty)
     * @param obj  dom元素
     * @returns 如果为true，则为空对象, false 不为空
     */
    isObjectEmpty:function(obj){
        for (var name in obj) {
            return false;
        }
        return true;
    },
    /**
     * 将元素添加到数组
     * @param ele
     * @param arr
     * @returns {*}
     */
    addToArray: function(ele, arr) {
        if (arr.length == 0) {
            arr.push(ele)
        } else {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] == ele) {
                    return arr
                }
            }
            arr.push(ele)
        }
        return arr
    },
    /**
     * 从数组中删除元素
     * @param arr
     * @param ele
     * @returns {*}
     */
    removeElement: function(arr, ele) {
        if (arr.length == 0) {
            return arr
        } else {
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] === ele) {
                    arr.splice(i, 1);
                }
            }
            return arr
        }
    },
    /**
     * 删除输入框左右两侧的空格
     * @param str
     * @returns {string|void|XML|*}
     */
    blankSpace:function(str){
        return str.replace(/(^\s*)|(\s*$)/g, "")
    },
    /**
     * 格式化日期显示
     * @param dateStr
     * @returns {string}
     */
    formatDate: function(dateStr) {
        if (dateStr == "") {
            return ""
        } else {
            var date = new Date(dateStr);
            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-'
                + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
        }
    },
    /**
     * 格式化日期显示无时分秒
     * @param dateStr
     * @returns {string}
     */
    formatDateTime: function(dateStr) {
        if (dateStr == "") {
            return ""
        } else {
            var date = new Date(dateStr);
            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-'
                + date.getDate();
        }
    },
    /**
     * 判断对象是否为null或者undefined
     * 判断对象是否为undefined
     * @param v
     * @returns {boolean}
     */
    isUndef: function(v) {
        if (typeof(v) == 'undefined') {
            return true
        } else {
            return false
        }
    },
    /**
     * 判断对象是否为null
     * @param v
     * @returns {boolean}
     */
    isNull: function(v) {
        if (!v && typeof v != "undefined" && v != 0) {
            return true
        } else {
            return false
        }
    },
    /**
     * 判断对象是否是undefined或者为空对象或者空字符串
     * @param v
     * @returns {*|boolean|如果为true}
     */
    isEmpty: function(v) {
        return this.isUndef(v) || this.isObjectEmpty(v) || this.isNull(v) ||  v === ""
    },
    /**
     * 判断对象是否为原始类型
     * @param value
     * @returns {boolean}
     */
    isPrimitive: function(value) {
        return (typeof value === 'string' ||
            typeof value === 'number' ||
            typeof value === 'boolean');
    },
    /**
     * 判断参数是否为对象
     * @param obj
     * @returns {boolean}
     */
    isObject: function(obj) {
        return obj !== null && typeof obj === 'object';
    },

    /**
     * 判断是否是数组或者[object HTMLCollection]
     * @param obj
     * @returns {boolean}
     */
    isArray: function(obj) {
        var type = Object.prototype.toString.call(obj);
        return type == '[object Array]' || type == '[object HTMLCollection]';
    },
    /**
     *为元素添加样式
     * @param obj
     * @param className
     */
    addClass: function(element, className) {
        if (!element.className || element.className == "") {
            element.className =className;
        } else {
            if(!this.hasClass(element, className)) {
                element.className += " " + className;
            }
        }
    },
    /**
     * 为元素删除样式
     * @param element
     * @param className
     */
    removeClass: function(element, className) {
        if (element.className && element.className != "") {
            var classArr = element.className.split(" ");
            element.className = this.removeElement(classArr, className).join(" ");
        }
    },
    /**
     * 判断元素是否拥有类
     * @param element
     * @param className
     * @returns {boolean}
     */
    hasClass: function(element, className) {
        var flag = false;
        if(!element.className) return flag;
        var classNameArr = element.className.split(" ");
        if(classNameArr.length == 0) return false
        for (var i = 0; i < classNameArr.length; i ++) {
            if(classNameArr[i] == className) {
                flag = true;
            }
        }
        return flag;
    },
    /**
     * 获取带有className类的所有父节点
     * @param element
     * @param className
     * @returns {Array}
     */
    getParents: function(element, className) {
        var arr = [];
        var currentNode;
        for(currentNode = element; !this.isUndef(currentNode); currentNode = currentNode.parentNode) {
            if(this.hasClass(currentNode, className)) {
                arr.push(currentNode);
            }
        }
        return arr;
    },

    /**
     * 获取带有className类的所有子节点
     * @param element
     * @param className
     * @returns {Array}
     */
    getChildren: function(element, className) {
        var arr = [];
        var childNodes;
        if(element && element.childNodes) {
            childNodes = element.childNodes;
        }
       if(childNodes && childNodes.length > 0) {
            for(var i = 0; i < childNodes.length; i ++) {
                if(this.hasClass(childNodes[i], className)) {
                    arr.push(childNodes[i]);
                }
                if(childNodes[i].childNodes && childNodes[i].childNodes.length > 0) {
                    arr = this.arrayMerge(arr, this.getChildren(childNodes[i], className));
                }
            }
       }
        return arr;
    },
    /**
     * 获取带有className类的所有下一级子节点
     * @param element
     * @param className
     * @returns {Array}
     */
    getChild: function(element, className) {
        var arr = [];
        var childNodes = element.childNodes;
        if(childNodes && childNodes.length > 0) {
            for(var i = 0; i < childNodes.length; i ++) {
                if (this.hasClass(childNodes[i], className)) {
                    arr.push(childNodes[i]);
                }
            }
        }
        return arr;
    },
    /**
     * 隐藏元素
     * @param any
     */
    hideElement: function(any) {
        if(this.isArray(any)) {
            for(var i = 0; i < any.length; i ++) {
                this.addClass(any[i], "hidden");
            }
        } else {
            this.addClass(any, "hidden")
        }
    },
    /**
     * 合并数组，不排重
     * @param arrA
     * @param arrB
     * @returns {*}
     */
    arrayMerge: function(arrA, arrB) {
        for(var i = 0; i < arrB.length; i ++) {
            arrA.push(arrB[i]);
        }
        return arrA;
    },
    /**
     * 显示元素
     * @param any
     */
    showElement: function(any) {
        if(this.isArray(any)) {
            for(var i = 0; i < any.length; i ++) {
                this.removeClass(any[i], "hidden");
            }
        } else {
            this.removeClass(any, "hidden")
        }
    },
    /**
     * 切换显示隐藏
     * @param any
     */
    toggleElement: function(any) {
        if(this.isArray(any)){
            for(var i = 0; i < any.length; i ++) {
                if(this.hasClass(any[i], "hidden")) {
                    this.removeClass(any[i], "hidden");
                } else {
                    this.addClass(any[i], "hidden");
                }
            }
        } else {
            if(this.hasClass(any, "hidden")) {
                this.removeClass(any, "hidden");
            } else {
                this.addClass(any, "hidden");
            }
        }
    },
    /**
     * 移除数组中的空元素
     * @param arr
     * @returns {*}
     */
    removeEmptyElement: function(arr) {
        for (var i = 0; i < arr.length; i ++) {
            if(this.isEmpty(arr[i])) {
                arr.splice(i, 1)
            }
        }
        return arr
    },
    /**
     * 绑定事件
     * @param element
     * @param e
     * @param fn
     */
    addListener: function(element, e, fn) {
        this.detachListener(element,e, fn)
        if (element.addEventListener) {
            element.addEventListener(e, fn, false);
        } else {
            element.attachEvent("on" + e, fn);
        }
    },
    /**
     * 解除事件绑定
     * @param element
     * @param e
     * @param fn
     */
    detachListener: function(element, e, fn) {
        if (element.removeEventListener) {
            element.removeEventListener(e, fn);
        } else {
            element.detachEvent("on" + e, fn);
        }
    },
    /**
     * 事件委派
     * @param event
     * @param className
     * @param callback
     */
    delegate: function(event, className, callback) {
        this.addListener(document, event, function(e) {
            if(VUE_UTIL.hasClass(e.target, className)){
                callback(e);
            }
        })
    },
    checkeRequired: function(data, arr) {

    },
    /**
     * 设置localStorage存储
     * @param key
     * @param value
     */
    setLocalStorage: function(key, value) {
        if (window.localStorage) {
            window.localStorage.setItem(key, value)
        } else {
            console.log("浏览器不支持localStorage")
        }
    },
    /**
     * 获取localStorage存储的值
     * @param key
     * @returns {null}
     */
    getLocalStorage: function(key) {
        if (window.localStorage) {
            return window.localStorage.getItem(key)
        } else {
            return null
        }
    },
    /**
     * 清除localStorage
     */
    clearLocalStorage: function() {
        if (window.localStorage) {
            window.localStorage.clear()
        }
    },
    /**
     * 获取用户信息JSON
     * @returns {null}
     */
    getUserInfo: function() {
        if (window.localStorage) {
            if(!this.isEmpty(this.getLocalStorage("userInfo"))) {
                return JSON.parse(this.getLocalStorage("userInfo"))
            }
        }
        return null
    },
    /**
     * 数组拷贝
     * @param arr
     * @returns {Array}
     */
    copyArray: function(arr) {
      var temp = []
      for(var i = 0; i < arr.length; i ++) {
          temp.push(arr[i])
      }
      return temp
    },
    /**
     * 使用循环的方式判断一个元素是否存在于一个数组中
     * @param {Object} arr 数组
     * @param {Object} value 元素值
     */
    isInArray:function(arr,value){
        for(var i = 0; i < arr.length; i++){
            if(value === arr[i]){
                return true;
            }
        }
        return false;
    }
};